import java.util.*;
public class of32 {
    public static void main(String[] args) {
        
    }
    public int[] levelOrder(TreeNode root) {
        Queue tn = new LinkedList<>();
        tn.offer(root);
        List a = new ArrayList<Integer>();
        while(tn.isEmpty()){
            TreeNode now=tn.poll();
            a.add(now.val);
            if(now!=null){
                tn.offer(now.peek().left);
                tn.offer(now.peek().right);
            }
        }
        int n=a.size();
        int b[]=new int[n];
        for(int i=0;i<n;i++){
            b[i]=(int)a.get(i);
        }
        return b;
    }
}
public int[] levelOrder(TreeNode root) {
    if(root == null) return new int[0];
    Queue<TreeNode> queue = new LinkedList<>(){{ add(root); }};
    ArrayList<Integer> ans = new ArrayList<>();
    while(!queue.isEmpty()) {
        TreeNode node = queue.poll();
        ans.add(node.val);
        if(node.left != null) queue.add(node.left);
        if(node.right != null) queue.add(node.right);
    }
    int[] res = new int[ans.size()];
    for(int i = 0; i < ans.size(); i++)
        res[i] = ans.get(i);
    return res;
}
//3241423